#pragma once

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include "Node.h"
using namespace std;
class NaviveMcC
{
public:
	void CreateTree(string* treeString, Node* root)
	{
		str = treeString;
		stringLength = treeString->length();
		string subStr;
		for (int i = 0; i < treeString->length(); i++)
		{
			subStr = treeString->substr(i);
			//cout << subStr << endl;
			insert(i, root, subStr);
		}
		
	}

	void Search(string find, vector<int> * result, Node* root){
		for each (Node* node in root->Children)
		{
			if (node->front(str) == find.front())
			{
				if (node->length() >= find.length())
				{
					int i = 0;
					for (; i < find.length(); i++)
					{
						if (node->at(i, str) != find.at(i))
						{
							return;
						}
					}
					leafNumber(node, result);
					sort(result->begin(),result->end());
					return;
				}
				else
				{
					int i = 0;
					for (; i < node->length(); i++)
					{
						if (node->at(i, str) != find.at(i))
						{
							return;
						}
					}
					Search(find.substr(i),result, node);
					return;
				}
			}
		}
	}


private:
	int stringLength;
	string * str;
	int number = 0;

	void leafNumber(Node* n, vector<int>* result)
	{
		if (n->suffix != -1)
		{
			result->push_back(n->suffix + 1);
		}
		else
		{
			for each (Node* node in n->Children)
			{
				leafNumber(node, result);
			}
		}
		return;
	}

	void insert(int stringIndex, Node* tree, string subStr)
	{
		if (tree->Children.size() > 0)
		{
			for each (Node* node in tree->Children)
			{
				if (node->front(str) == subStr.front())
				{
					if (node->length() >= subStr.length())
					{
						int i = 0;
						for (; i < subStr.length(); i++)
						{
							if (node->at(i,str) != subStr.at(i))
							{
								node->InsertChild(i, new Node(number++, stringIndex + i, stringLength));
								return;
							}
						}
						node->Children.push_back(new Node(number++, stringIndex + i, stringLength));
						return;
					}
					else
					{
						int i = 0;
						for (; i < node->length(); i++)
						{
							if (node->at(i,str) != subStr.at(i))
							{
								Node *n = new Node(number++, stringIndex + i, stringLength);
								node->InsertChild(i, n);
								return;
							}
						}
						insert(stringIndex+1, node, subStr.substr(i));
						return;
					}
				}
				
				
			}

			

		}
		
		tree->Children.push_back(new Node(number++, stringIndex, stringLength));
		
	}
};

